<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <table border="1">
        <caption>
          员工列表
        </caption>
        <tr>
          <th>编号</th>
          <th>姓名</th>
          <th>工资</th>
          <th>工作</th>
          <th>操作</th>
        </tr>
        <tr v-for="(e,index) in arr">
          <td>{{ index+1 }}</td>
          <td>{{ e.name }}</td>
          <td>{{ e.salary }}</td>
          <td>{{ e.job }}</td>
          <td><input type="button" value="删除" @click="del(index)" /></td>
        </tr>
      </table>
      <input type="text" v-model="emp.name" placeholder="姓名" />
      <input type="text" v-model="emp.salary" placeholder="工资" />
      <input type="text" v-model="emp.job" placeholder="工作" />
      <input type="button" value="添加员工" @click="f()" />
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      let app = new Vue({
        el: "body > div",
        data: {
          arr: [],
          emp: { name: "", salary: "", job: "" },
        },
        methods: {
          f() {
            app.arr.push({
              name: app.emp.name,
              salary: app.emp.salary,
              job: app.emp.job,
            });
          },
          del(index){
            // 从数组中删除对象 splice(删除的位置，删除的数量)
            app.arr.splice(index, 1);
          }
        },
      });
    </script>
  </body>
</html>
